﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Learn001.Controls
{
    [TemplatePart(Name = PanelImage, Type = typeof(Image))]
    [TemplatePart(Name = PanelDescript, Type = typeof(TextBlock))]
    public class GrayPanel : CustomPanel
    {
        protected internal const string PanelImage = "img";
        protected internal const string PanelDescript = "txtDescript";

        protected internal Image img;
        protected internal TextBlock txtDescript;

        /// <summary> 
        /// 按钮图片 
        /// </summary> 
        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(GrayPanel), new PropertyMetadata(null, new PropertyChangedCallback(ImageChanged)));

        /// <summary> 
        /// 按钮图片 
        /// </summary> 
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        private static void ImageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            GrayPanel panel = sender as GrayPanel;
            if (panel == null) return;

            if (e.NewValue != null && panel.img != null)
                panel.img.Source = (ImageSource)e.NewValue;
            else if (panel.img != null)
                panel.img.Source = null;
        }

        public static readonly DependencyProperty TitleDescriptProperty =
            DependencyProperty.Register("TitleDescript", typeof(string), typeof(GrayPanel), new PropertyMetadata("", new PropertyChangedCallback(TitleDescriptChanged)));

        public string TitleDescript
        {
            get { return (string)GetValue(TitleDescriptProperty); }
            set { SetValue(TitleDescriptProperty, value); }
        }

        private static void TitleDescriptChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            GrayPanel panel = sender as GrayPanel;
            if (panel == null) return;

            if (e.NewValue != null && panel.txtDescript != null)
                panel.txtDescript.Text = e.NewValue.ToString();
            else if (panel.txtDescript != null)
                panel.txtDescript.Text = "";
        }

        public GrayPanel()
            : base()
        {
            this.DefaultStyleKey = typeof(GrayPanel);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            img = base.GetTemplateChild(PanelImage) as Image;
            txtDescript = base.GetTemplateChild(PanelDescript) as TextBlock;

            if (img != null)
                img.Source = Image;

            if (txtDescript != null)
                txtDescript.Text = TitleDescript;
        }
    } 
}
